package com.lining.orangeservice.mapper;

import com.lining.orangeservice.entity.po.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 根据用户名查询用户（包含基础信息）
     */
    @Select("SELECT id, username, password, nickname, email, phone, avatar, status, " +
            "last_login_time, last_login_ip, create_time, update_time, create_by, update_by, deleted " +
            "FROM sys_user WHERE username = #{username} AND deleted = 0")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "nickname", column = "nickname"),
        @Result(property = "email", column = "email"),
        @Result(property = "phone", column = "phone"),
        @Result(property = "avatar", column = "avatar"),
        @Result(property = "status", column = "status"),
        @Result(property = "lastLoginTime", column = "last_login_time"),
        @Result(property = "lastLoginIp", column = "last_login_ip"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time"),
        @Result(property = "createBy", column = "create_by"),
        @Result(property = "updateBy", column = "update_by"),
        @Result(property = "deleted", column = "deleted")
    })
    User selectByUsername(@Param("username") String username);

    /**
     * 根据ID查询用户
     */
    @Select("SELECT id, username, password, nickname, email, phone, avatar, status, " +
            "last_login_time, last_login_ip, create_time, update_time, create_by, update_by, deleted " +
            "FROM sys_user WHERE id = #{id} AND deleted = 0")
    User selectById(@Param("id") Long id);

    /**
     * 更新用户登录信息
     */
    @Update("UPDATE sys_user SET last_login_time = #{loginTime}, last_login_ip = #{loginIp} " +
            "WHERE id = #{id}")
    int updateLoginInfo(@Param("id") Long id, 
                       @Param("loginTime") LocalDateTime loginTime, 
                       @Param("loginIp") String loginIp);

    /**
     * 插入新用户
     */
    @Insert("INSERT INTO sys_user (username, password, nickname, email, phone, avatar, status, create_by) " +
            "VALUES (#{username}, #{password}, #{nickname}, #{email}, #{phone}, #{avatar}, #{status}, #{createBy})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    /**
     * 检查用户名是否存在
     */
    @Select("SELECT COUNT(*) FROM sys_user WHERE username = #{username} AND deleted = 0")
    int countByUsername(@Param("username") String username);

    /**
     * 检查邮箱是否存在
     */
    @Select("SELECT COUNT(*) FROM sys_user WHERE email = #{email} AND deleted = 0")
    int countByEmail(@Param("email") String email);

    /**
     * 更新用户信息
     */
    @Update({"<script>",
        "UPDATE sys_user",
        "<set>",
        "  <if test='username != null'>username = #{username},</if>",
        "  <if test='password != null'>password = #{password},</if>",
        "  <if test='nickname != null'>nickname = #{nickname},</if>",
        "  <if test='email != null'>email = #{email},</if>",
        "  <if test='phone != null'>phone = #{phone},</if>",
        "  <if test='avatar != null'>avatar = #{avatar},</if>",
        "  <if test='status != null'>status = #{status},</if>",
        "  update_time = NOW(),",
        "  update_by = #{updateBy}",
        "</set>",
        "WHERE id = #{id} AND deleted = 0",
        "</script>"})
    int update(User user);
}